import { Controller, Get, Post, Body, Param, Delete, Session, HttpException, HttpStatus } from '@nestjs/common';
import { CronService } from './cron.service';

@Controller('cron')
export class CronController {
  constructor(private readonly cronService: CronService) { }

  @Get('list')
  async findAll(@Session() session: Record<string, any>) {
    if (!session.userId) {
      throw new HttpException('未登录', HttpStatus.UNAUTHORIZED);
    }

    const crons = await this.cronService.findAll();
    return {
      success: true,
      data: crons
    };
  }

  @Post('add')
  async create(@Body() createCronDto: any, @Session() session: Record<string, any>) {
    if (!session.userId) {
      throw new HttpException('未登录', HttpStatus.UNAUTHORIZED);
    }

    const cron = await this.cronService.create(createCronDto);
    return {
      success: true,
      data: cron
    };
  }

  @Post('update/:id')
  async update(@Param('id') id: string, @Body() updateCronDto: any, @Session() session: Record<string, any>) {
    if (!session.userId) {
      throw new HttpException('未登录', HttpStatus.UNAUTHORIZED);
    }

    const cron = await this.cronService.update(+id, updateCronDto);
    return {
      success: true,
      data: cron
    };
  }

  @Delete('delete/:id')
  async remove(@Param('id') id: string, @Session() session: Record<string, any>) {
    if (!session.userId) {
      throw new HttpException('未登录', HttpStatus.UNAUTHORIZED);
    }

    await this.cronService.remove(+id);
    return {
      success: true,
      data: null
    };
  }
} 